﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Net.Sockets;
using System.Threading;


namespace eHouse
{
    public class MainViewModel : INotifyPropertyChanged
    {
        public MainViewModel()
        {
            this.LightItems = new ObservableCollection<ItemViewModel>();
            this.WindowItems = new ObservableCollection<ItemViewModel>();
            this.TemperatureItems = new ObservableCollection<ItemViewModel>();
            this.Degrees = "N/A";
        }

        public ObservableCollection<ItemViewModel> LightItems { get; set; }
        public ObservableCollection<ItemViewModel> WindowItems { get; set; }
        public ObservableCollection<ItemViewModel> TemperatureItems { get; set; }
        public string Degrees { get; set; }
        public bool Irrigation { get; set; }

        private AutoResetEvent wait;

        /// <summary>
        /// Creates and adds a few ItemViewModel objects into the Items collection.
        /// </summary>
        public void LoadData()
        {
            //if (IsDataLoaded)
            //{
            //    return;
            //}

            using (wait = new AutoResetEvent(false))
            {
                App.ConnectionManager.GetLights(OnLightsReceived);
                wait.WaitOne();
                App.ConnectionManager.GetWindows(OnWindowsReceived);
                wait.WaitOne();
                App.ConnectionManager.GetTemperature(OnTemperatureReceived);
                wait.WaitOne();
                App.ConnectionManager.GetIrrigation(OnIrrigationReceived);
                wait.WaitOne();
            }
        }

        private void OnLightsReceived(object sender, SocketAsyncEventArgs e)
        {
            if (sender == null)
            {
                try
                {
                    wait.Set();
                }
                catch (Exception) { }
                return;
            }
            LightItems = new ObservableCollection<ItemViewModel>();
            Dictionary<string, bool> lights = sender as Dictionary<string, bool>;
            foreach (KeyValuePair<string, bool> pair in lights)
            {
                LightItems.Add(new ItemViewModel() { LineOne = pair.Key, SwitchState = pair.Value.ToString() });
            }
            wait.Set();
        }

        private void OnWindowsReceived(object sender, SocketAsyncEventArgs e)
        {
            if (sender == null)
            {
                try
                {
                    wait.Set();
                }
                catch (Exception) { }
                return;
            }
            WindowItems = new ObservableCollection<ItemViewModel>();
            Dictionary<string, bool> windows = sender as Dictionary<string, bool>;
            foreach (KeyValuePair<string, bool> pair in windows)
            {
                WindowItems.Add(new ItemViewModel() { LineOne = pair.Key, SwitchState = pair.Value.ToString() });
            }
            wait.Set();
        }

        private void OnTemperatureReceived(object sender, SocketAsyncEventArgs e)
        {
            if (sender == null)
            {
                try
                {
                    wait.Set();
                }
                catch (Exception) { }
                return;
            }
            TemperatureItems = new ObservableCollection<ItemViewModel>();
            Dictionary<string, string> tempSensors = sender as Dictionary<string, string>;
            Degrees = "Current: " + tempSensors["VALUE"] + " ºC";
            tempSensors.Remove("VALUE");
            foreach (KeyValuePair<string, string> pair in tempSensors)
            {
                TemperatureItems.Add(new ItemViewModel() { LineOne = pair.Key, SwitchState = (pair.Value == "TRUE").ToString() });
            }
            wait.Set();
        }

        private void OnIrrigationReceived(object sender, SocketAsyncEventArgs e)
        {
            if (sender == null)
            {
                try
                {
                    wait.Set();
                }
                catch (Exception) { }
                return;
            }
            Irrigation = "TRUE".Equals(sender);
            wait.Set();
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}